package com.yanqu.road.server.gameplayer.module.activity.tomb.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import io.netty.util.internal.StringUtil;

import java.util.List;

public class TombUserCostItemServiceCmd implements IGameServiceCommand {

    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(request.getSenderId());
        if (onlinePlayer == null) {
            syncResponse.setErrorCode(GameErrorCode.E_TOMB_USER_NOT_FOUND);
            return syncResponse;
        }
        Tomb.TombUserCostItemReq req = Tomb.TombUserCostItemReq.parseFrom(request.getDataBytes());

        if (StringUtil.isNullOrEmpty(req.getRewards())) {
            syncResponse.setErrorCode(GameErrorCode.E_ERROR_PARAMETER);
            return syncResponse;
        }

        Property property = PropertyHelper.parseStringToProperty(req.getRewards());
        boolean b = onlinePlayer.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Tomb, eLogMoneyType.TombEventCost);
        if (!b) {
            syncResponse.setErrorCode(GameErrorCode.E_TOMB_EVENT_NOT_ENOUGH_ITEM);
            return syncResponse;
        }

        return syncResponse;
    }
}
